在一個 靜態類型語言,不可變性是資料的基本狀態,確保記憶體安全並實現可預測的執行。雖然變數可以被遮蔽或設為可變, 常數 但常數更進一步地將值永久綁定至名稱上。
1. 常數的嚴謹性
與標準變數不同,當 類型推論 允許編譯器推斷資料類型時,常數則必須明確指定類型 明確的類型註解 (例如, : u32)。這確保了程式碼二進位檔內的嚴謹合約。
2. 編譯時期計算
常數不僅僅是不可變的變數;它們會在編譯期間被計算並「烘焙」到程式的二進位檔中。這使得編譯器可以在程式執行前就進行 常數運算式 (例如 60 * 60 * 3)的計算,從而優化效能。
3. 絕對不可變性
常數作為「唯一可信來源」。它們無法使用 mut 使其可變,也不能在同一作用域中被遮蔽,確保程式的核心參數在整個執行過程中都保持不變。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following is REQUIRED when declaring a constant in Rust?
The 'mut' keyword
A type annotation (e.g., : u32)
A semicolon before the name
The 'let' keyword
✅ Correct!
Rust constants must always have their type explicitly stated; the compiler will not use type inference for them.❌ Incorrect
Unlike 'let' variables, constants demand explicit type annotations to satisfy the language's safety contracts.QUESTION 2
When are the values of constants calculated?
At runtime, when the variable is first accessed.
During program initialization only.
At compile-time.
Only when the user provides input.
✅ Correct!
Constants are evaluated at compile-time, allowing the compiler to optimize the program binary with the result.❌ Incorrect
Calculations for constants happen before the program ever runs, which is why they are limited to constant expressions.QUESTION 3
Can a constant be shadowed in the same scope?
Yes, using the 'let' keyword.
Yes, if the value is the same type.
No, constants cannot be shadowed in the same scope.
Only within a function body.
✅ Correct!
Constants are absolutely immutable and cannot be shadowed in the same scope, serving as a permanent source of truth.❌ Incorrect
Shadowing applies to variables declared with 'let', not to 'const' definitions.QUESTION 4
What is a 'Constant Expression'?
An expression that returns a random number.
An expression that can be computed at compile-time.
A string literal that cannot be formatted.
A mathematical formula used only in 'mut' variables.
✅ Correct!
Constant expressions are logic or math (like 60 * 60) that the compiler resolves during the build process.❌ Incorrect
Constant expressions must be deterministic and resolvable by the compiler without running the program.QUESTION 5
Which keyword is used to define a constant in Rust?
let immutable
define
const
static_val
✅ Correct!
The 'const' keyword is specifically used for these permanent, compile-time values.❌ Incorrect
In Rust, 'const' is the reserved keyword for absolute constants.Module: Security & Performance via Constants
Optimizing a Web Server's Configuration
You are developing a high-performance web server. You need to define a session timeout of 3 hours that is used across multiple modules. To prevent any logic from accidentally changing this duration and to ensure maximum runtime speed, you decide to use a constant.
Q
1. Write the Rust code to define 'THREE_HOURS_IN_SECONDS' as a u32 constant.
Solution:
Note the mandatory type and the use of math that the compiler will solve.
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;Note the mandatory type and the use of math that the compiler will solve.
Q
2. Why is this more performant than using a standard variable?
Solution:
Because the value is computed at compile-time. The program doesn't waste CPU cycles at runtime doing the multiplication; it simply loads the final result (10800) directly from the binary.
Because the value is computed at compile-time. The program doesn't waste CPU cycles at runtime doing the multiplication; it simply loads the final result (10800) directly from the binary.
Q
3. If a developer tries to use shadowing or the 'mut' keyword on this constant, what happens?
Solution:
The compiler will throw an error. Constants do not support 'mut', and shadowing rules for variables do not apply to constants in the same way, preventing accidental state corruption.
The compiler will throw an error. Constants do not support 'mut', and shadowing rules for variables do not apply to constants in the same way, preventing accidental state corruption.